# coding: utf-8
from typing import Literal

from PySide6.QtCore import QSize
from PySide6.QtGui import QColor
from PySide6.QtWidgets import QApplication, QWidget
from qfluentwidgets import SplashScreen, NavigationItemPosition, FluentWindow, FluentIcon, InfoBar

from common import SingleIcon, cfg, Icon, TotalWorkerObject, signalBus, sqlRequest
from components import PictureBrowserDialog
from server.db import add_history_view
from .bird import BirdInterface
from .history import HistoryInterface
from .lenovo import LenovoInterface
from .search import SearchInterface
from .setting import SettingInterface


class MainWindow(FluentWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.initWindow()
        self.initThreads()

        self.birdInterface = BirdInterface(self)
        self.lenovoInterface = LenovoInterface(self)
        # self.mktpInterface = MktpInterface(self)
        self.searchInterface = SearchInterface(self)
        self.historyInterface = HistoryInterface(self)
        self.settingInterface = SettingInterface(self)

        self.initSubInterface()
        self.connectSignals()

    def initWindow(self):
        self.resize(1280, 700)
        self.setMinimumWidth(1280)
        self.setWindowIcon(SingleIcon.LOGO_SVG.icon())
        self.setWindowTitle('Wallpaper Engine')

        self.setCustomBackgroundColor(QColor(240, 244, 249), QColor(32, 32, 32))
        self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))
        self.navigationInterface.setExpandWidth(200)

        # create splash screen
        self.splashScreen = SplashScreen(self.windowIcon(), self)
        self.splashScreen.setIconSize(QSize(106, 106))
        self.splashScreen.raise_()

        desktop = QApplication.primaryScreen().availableGeometry()
        w, h = desktop.width(), desktop.height()
        self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
        self.show()
        QApplication.processEvents()
        self.splashScreen.finish()

    def initThreads(self):
        self.totalWorkerObject = TotalWorkerObject(self)

    def initSubInterface(self):
        self.addSubInterface(self.birdInterface, SingleIcon.BIRD, "小鸟")
        self.addSubInterface(self.lenovoInterface, SingleIcon.LENOVO, "Lenovo")
        # self.addSubInterface(self.mktpInterface, SingleIcon.MKTP, "免抠图")
        self.addSubInterface(self.searchInterface, FluentIcon.SEARCH, "搜索")

        pos = NavigationItemPosition.BOTTOM
        self.addSubInterface(self.historyInterface, FluentIcon.HISTORY, "历史", position=pos)
        self.addSubInterface(self.settingInterface, Icon.SETTINGS, "设置", position=pos)

    def connectSignals(self):
        signalBus.showImageSignal.connect(self.showPictureBrowser)
        signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)
        signalBus.msgSignal.connect(self.showMsgBox)

    def showPictureBrowser(self, title: str, desc: str, url: str):
        dialog = PictureBrowserDialog(self)
        sqlRequest(add_history_view, title=title, description=desc, url=url)
        dialog.setUrl(url)
        dialog.show()

    def switchTo(self, interface: QWidget):
        super().switchTo(interface)
        if interface == self.lenovoInterface:
            self.lenovoInterface.loadInitData()
        elif interface == self.historyInterface:
            self.historyInterface.onReload()

    def showMsgBox(self, level: Literal['info', 'success', 'warning', 'error'], msg: str):
        if level == 'info':
            InfoBar.info('提示', msg, duration=2000, parent=self.window())
        elif level == 'success':
            InfoBar.success('成功', msg, duration=2000, parent=self.window())
        elif level == 'warning':
            InfoBar.warning('警告', msg, duration=2000, parent=self.window())
        elif level == 'error':
            InfoBar.error('错误', msg, duration=3000, parent=self.window())
